package RPC;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.Socket;

public class ExportTask implements Runnable {
	Socket client = null;
	public ExportTask(Socket socket){
		client = socket;
	}

	@Override
	public void run() {
		ObjectInputStream oins = null;
		ObjectOutputStream oous = null;
		try{
			oins = new ObjectInputStream(client.getInputStream());
			String interfaceName = oins.readUTF();
			Class<?> service = Class.forName(interfaceName);
			String methodName = oins.readUTF();
			Class<?>[] parameterTypes = (Class<?>[])oins.readObject();
			Object[] arguments = (Object[])oins.readObject();
			Method method = service.getMethod(methodName, parameterTypes);
			Object result = method.invoke(service.newInstance(), arguments);
			oous = new ObjectOutputStream(client.getOutputStream());
			oous.writeObject(result);
		}catch (Exception e) {
			
		}finally {
			
				try {
					if(oins != null)
						oins.close();
					if(oous != null)
						oous.close();
					if(client != null)
						client.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

}
